package com.cost.service.impl;

import com.cost.dao.CostRechargeDao;
import com.cost.service.CostRechargeService;
import com.cost.vo.Entity;
import com.rmb.mart.entity.cost.CostRecharge;
import com.rmb.mart.utils.IdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (CostRecharge)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 14:04:18
 */
@Service("costRechargeService")
@Transactional(rollbackFor = Exception.class)
public class CostRechargeServiceImpl implements CostRechargeService {
    @Resource
    private CostRechargeDao costRechargeDao;

    @Resource
    private IdWorker idWorker;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CostRecharge queryById(Long id) {
        return this.costRechargeDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CostRecharge> queryAllByLimit(int offset, int limit) {
        return this.costRechargeDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param costRecharge 实例对象
     * @return 实例对象
     */
    @Override
    public CostRecharge insert(CostRecharge costRecharge) {
        this.costRechargeDao.insert(costRecharge);
        return costRecharge;
    }

    /**
     * 修改数据
     *
     * @param costRecharge 实例对象
     * @return 实例对象
     */
    @Override
    public CostRecharge update(CostRecharge costRecharge) {
        this.costRechargeDao.update(costRecharge);
        return this.queryById(costRecharge.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.costRechargeDao.deleteById(id) > 0;
    }

    /**
     * 查询所用数据
     *
     * @return 所用数据
     */

    @Override
    public List<CostRecharge> selectAll() {
        return this.costRechargeDao.queryAll(null);
    }

    @Override
    public List<CostRecharge> selectAllCondition(CostRecharge costRecharge) {
        List<CostRecharge> costRecharges = new ArrayList<>();
        if (costRecharge!=null){
            Long uid = costRecharge.getUid();
            if (uid!=null){
                costRecharges = this.costRechargeDao.queryAll(costRecharge);
            }
            return costRecharges;
        }else {
            return costRecharges;
        }
    }

    @Override
    public void recharg(Long id, Double balance) {

        Entity entity = this.costRechargeDao.selectFindUid(id);

        BigDecimal decimal = new BigDecimal(balance);

        BigDecimal bigDecimal = entity.getBalance().add(decimal);

        this.costRechargeDao.updateBalance(id,bigDecimal);

        CostRecharge costRecharge = new CostRecharge();
        costRecharge.setRechargeId(idWorker.nextId());
        costRecharge.setDealPrice(balance);
        costRecharge.setRechargeDate(new Date());
        this.costRechargeDao.insert(costRecharge);
    }

}